# -*- coding: utf-8 -*-

import cv2
import numpy as np
from matplotlib import pyplot as plt

# #礼帽 = 原始输入 - 开运算结果
# img = cv2.imread('dige.png')
# kernel = np.ones((5,5),np.uint8)
# tophat = cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
#
# res = np.hstack((img, tophat))
# cv2.imshow('erosion', res)
# cv2.imwrite('tophat.png', res)
# #gradient为原图、礼帽对比图
# cv2.waitKey(0)
# cv2.destroyAllWindows()

#############################################
# 黑帽 = 闭运算 - 原始输入
# img = cv2.imread('dige.png')
# kernel = np.ones((5,5),np.uint8)
# blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
#
# res = np.hstack((img, blackhat))
# cv2.imshow('erosion', res)
# cv2.imwrite('blackhat.png', res)
# #gradient为原图、礼帽对比图
# cv2.waitKey(0)
# cv2.destroyAllWindows()
##############################################
img = cv2.imread('dige.png')
kernel = np.ones((5,5),np.uint8)
#礼帽 = 原始输入 - 开运算结果
tophat = cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
# 黑帽 = 闭运算 - 原始输入
blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
res = np.hstack((img, tophat,blackhat))
cv2.imshow('erosion', res)
cv2.imwrite('hat.png', res)
#hat为原图、礼帽对比图
cv2.waitKey(0)
cv2.destroyAllWindows()